package com.sinba.itsm;

import com.sinba.common.system.dto.RuleDTO;
import com.sinba.common.system.service.IRuleService;
import com.sinba.common.util.StringUtils;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 会话过滤方法
 * @author Guoyu_Tan
 *
 */
public class SessionFilter
    implements Filter
{
	
    public void destroy(  )
    {
    }

    public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain )
                  throws IOException, ServletException
    {
        try
        {
            HttpSession session = ( (HttpServletRequest) request ).getSession(  );
            HttpServletResponse res = (HttpServletResponse) response;
            HttpServletRequest req = (HttpServletRequest) request;

            String url = req.getRequestURI(  );
            
            if(url.indexOf("/user")!=-1){
            	ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
            	IRuleService ruleService = (IRuleService)ac.getBean("ruleService");
            	RuleDTO ruleDTO = ruleService.findRuleInfo();
            	if(ruleDTO.isRule021()){
            		 res.sendError( 707 );
                     return;
            	}
            }
            
            
            String userName = (String) session.getAttribute( "loginName" );
            if ( ! StringUtils.hasText( userName ) && ! filterPage( url ) && ( url.indexOf( "admin" ) == -1)){ //如果session有用户名证明还是登录中的
                res.sendError( 606 );
                return;
            } else
            {
                String regUserStatus = (String) session.getAttribute( "regUserStatus" );
            	//if ( !url.endsWith("registerUser!saveRegisterInfo.action")
            	if ( (url.endsWith("user/index.jsp") || url.endsWith("user/"))
            			&& regUserStatus != null && "unfinished".equals(regUserStatus)) {
            		res.sendError(699); 
                    return;
				}
                chain.doFilter( request, response );
                return;
            }
        } catch ( Exception ex )
        {
            ex.printStackTrace(  );
        }
    }

    private String[] strs =
        new String[]
        {
            "login.jsp","logout.jsp", "getCode.jsp", "registerUser!saveRegisterUserAuth.action", "registerUser!login.action",
            "registerUser!findUserPassword.action", "registerUser!login2.action", "user!userLogin.action",
            "updateUserPassword.action", "index.jsp", "fp.jsp", "registerUser!findUserPassword.action",
            "findPasswordSuccess.jsp", "404.jsp", "500.jsp", "606.jsp", "707.jsp", "bottom.jsp","/",
            "registerUser!getBack.action",
            "news!newsInfoOut.action",
            "newsList-out.jsp",
            "reg1.jsp","reg.jsp",
            "register.jsp","activationCode!getEmailActivationCode.action",
            "activationCode!authEmailCode.action",
            "registerUser!saveRegisterUserAuth.action",
            "registerUser!saveRegiserUser.action",
            "registerUser!regiserUser.action",
            "registerUser!findContactNo.action",
            "top.jsp",
            "bottom.jsp",
            "about_us.jsp",
            "services.jsp",
            "portfolio.jsp",
            "contact.jsp",
            "findPwd.jsp",
            "payadvice.jsp",
            "rules.jsp",
            "app!login.action",
            "app!userReg.action",
            "app!autoFindContactNo.action",
            "app!findAccountInfo.action",
            "app!findUserInfo.action",
            "app!findMyTeam.action",
            "app!withdraw.action",
            "app!withdrawHistory.action",
            "app!exchange.action",
            "app!exchangeHistory.action",
            "app!bonusDetails.action",
            "app!financingRule.action",
            "app!userActivation.action",
            "app!logout.action",
            "app!getTeamActiveQuy.action",
            "app!bonusSum.action",
            "app.jsp",
            "DialogContact.jsp",
            "rec.jsp",
            "registerUser!findUserNameByLoginName.action",
            "bonus!everyDayHong.action",
            "lottery.jsp",
            "registerCode!lotteryRegisterCode.action",
            "bonus!everyDayHong.action"
        };

    public boolean filterPage( String url )
    {
        boolean result = false;

        for ( int i = 0; i < strs.length; i++ )
        {
            if ( url.endsWith( strs[i] ) || url.equals(strs[i]) )
            {
                result = true;
            }
        }

        return result;
    }

    public void init( FilterConfig fConfig )
              throws ServletException
    {
    }
}
